package com.sumainfor.pc.dev.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.dev.dao.CouponMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZwxTenantFeign;
import com.sumainfor.pc.dev.service.CouponLogService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券创建记录
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-05 11:17
 **/
@Controller
@RequestMapping("/couponLog")
public class CouponLogController {

    @Autowired
    CouponLogService couponLogService;

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    ZwxTenantFeign zwxTenantFeign;
    /**
    * <p>
    *查看优惠券生产批次的详情
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @GetMapping("/getCouponLogCouType")
    public JsonResult getCouponLogCouType(@RequestParam Map<String,Object> params, Pager pager){
        return couponLogService.getCouponLogCouType(params,pager);
    }

    /**
    * <p>
    * 查看同一批次下优惠券的核销情况
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @GetMapping("/getCouponBatIdList")
    public JsonResult getCouponBatIdList(@RequestParam Map<String,Object> params,Pager pager) {
        JsonResult result = new JsonResult();
        System.out.println("params" + params);
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout = MapUtils.getInteger(params, "Circulation");
        System.out.println("cout" + cout);
        if (cout>0) {
            List<Map<String, Object>> getCouponBatIdList = couponMapper.getCouponBatIdList(params);
            System.out.println("getCouponBatIdList"+getCouponBatIdList);
            for (Map<String,Object> paramsMap:getCouponBatIdList) {
                if (paramsMap.get("TenId") !=null){
                    JsonResult tenPhoneResult = zwxTenantFeign.getTenantTenPhone(paramsMap);
                    Map<String,Object> tenPhoneMap= (Map<String, Object>) tenPhoneResult.get("result");
                 paramsMap.putAll(tenPhoneMap);
                    System.out.println("paramsMap"+paramsMap);
                }
            }
            return pageUtils.getJsonResult(getCouponBatIdList,params,cout);

        }


        return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);

    }

    /**
    * <p>
    * 创建优惠券（批次）
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/insertCouponLog")
    public JsonResult insertCouponLog(@RequestBody Map<String,Object> params){
        return couponLogService.insertCouponLog(params);
    }

}
